/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.jpdf.handler;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Objects;

import com.hyts.jpdf.config.PdfConfig;
import com.hyts.jpdf.element.PdfOperaElement;
import com.hyts.jpdf.utility.FreemarkerUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;

/** 
 * @title DefaultPdfHandler.java  
 * @package com.hyts.jpdf.handler  
 * @description 默认的pdf操作处理类  
 * @see com.hyts.jpdf.handler.AbstractPdfHandler<PdfOperaElement>
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月16日 
 * @version V1.0 
 */
public class DefaultPdfHandler extends AbstractPdfHandler<PdfOperaElement> {

	/* (非 Javadoc)
	 * @param outputFile
	 * @param param
	 * @return
	 * @see com.hyts.jpdf.handler.AbstractPdfHandler#createPdf(java.lang.String, java.lang.Object)  
	 * @exception
	 */ 
	@Override
	public boolean createPdf(String outputFile, PdfOperaElement param) {
		Document document = new Document();
		PdfWriter writer = null;
		try {
			writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
			document.open();
	        Font f1 = FontFactory.getFont(PdfConfig.DEFAULT_PDF_FILE_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			document.add(new Paragraph(param.getContent(),f1));
		} catch (DocumentException | FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			document.close();
			if(Objects.nonNull(writer)){
				writer.close();
			}
		}
		return false;
	}

	/* (非 Javadoc)
	 * @param htmlFilePath
	 * @param outputFile
	 * @return
	 * @see com.hyts.jpdf.handler.AbstractPdfHandler#htmlToPdf(java.lang.String, java.lang.String)  
	 * @exception
	 */ 
	@Override
	public boolean htmlToPdf(String htmlFilePath, String outputFile) {
		 // step 1
        Document document = new Document();
        try {
        	XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        	fontImp.register(PdfConfig.DEFAULT_PDF_FILE_FONT_PATH);
        	// step 2
        	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        	// step 3
        	document.open();
        	// step 4
			XMLWorkerHelper.getInstance().parseXHtml(writer, document,
			        new FileInputStream(htmlFilePath), null, Charset.forName(PdfConfig.DEFAULT_PDF_HTML_CHARSET), fontImp);
		} catch (IOException | DocumentException e) {
			e.printStackTrace();
		}
        // step 5
        document.close();
		return false;
	}

	/* (非 Javadoc)
	 * <b>TODO(方法功能的描述)-  freemarkerToPdf </b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param templatePath
	 * @param outputFile
	 * @return
	 * @see com.hyts.jpdf.handler.AbstractPdfHandler#freemarkerToPdf(java.lang.String, java.lang.String)  
	 * @exception
	 */ 
	@Override
	public boolean freemarkerToPdf(String templatePath,Map<String,Object> data,String outputFile) {
		Document document = new Document();
        try {
        	String content = FreemarkerUtil.freeMarkerRender(data, templatePath);
        	// step 1
        	// step 2
        	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        	// step 3
        	document.open();
        	// step 4
        	XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        	fontImp.register(PdfConfig.DEFAULT_PDF_FILE_FONT_PATH);
			XMLWorkerHelper.getInstance().parseXHtml(writer, document,
			        new ByteArrayInputStream(content.getBytes()), null, Charset.forName(PdfConfig.DEFAULT_PDF_HTML_CHARSET), fontImp);
		} catch (IOException | DocumentException e) {
			e.printStackTrace();
		} finally{
			document.close();
		}
        // step 5
		return false;
	}

}
